package cn.piesat.scanning.business.xz.service.lkrimpl;

import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildLAKE_WATERServiceImpl implements BuildAlgorithmTaskService {
    
    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath,
                                                   AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam,
                                                   Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
//日尺度湖泊水体面积提取算法
        //1为日尺度,2为季尺度
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] s = fileName.split("_");
        Map<String, Object> params = new HashMap<>();
        if (taskPathParam.getParameter06().equals("1")) {

            String startTime = s[5] + "000000";
            String endTime = s[5] + "235959";
            //GF1_WFV1_GBAL_L1F_GLL_20230721_114307_16M_MSS.json
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findByMetadata(startTime, endTime, s[3], s[0],
                    s[3], s[2], "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            String join = StrUtil.join(",", collect);
            params.put("primaryFile", join);
            params.put("auxPath", taskPathParam.getParameter02());
            params.put("primaryResult", "");
            params.put("clipRegion", taskPathParam.getParameter03());
            params.put("regionId", taskPathParam.getParameter04());
            params.put("regionName", taskPathParam.getParameter05());
            params.put("aloType", Integer.parseInt(taskPathParam.getParameter06()));
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        } else {
            //LKR_LADM_YZYC_L3_GF1_WFV1_0016M_GLL_MOAD_20230624000000_20230624235959.SHP
            String month = s[9].substring(4, 6); // 假设月份信息在日期字符串的第5位和第6位
            //3-4-5    6-7-8  9-10-11  12-1-2
            String quarter = null;
            switch (month) {
                case "03":
                case "04":
                case "05":
                    quarter = "1";
                    break;
                case "06":
                case "07":
                case "08":
                    quarter = "2";
                    break;
                case "09":
                case "10":
                case "11":
                    quarter = "3";
                    break;
                case "12":
                case "01":
                case "02":
                    quarter = "4";
                    break;
            }
            String year = s[9].substring(0, 4);
            String startTime = null, endTime = null;
            if ("1".equals(quarter)) {
                startTime = year + "0301000000";
                endTime = year + "0531235959";
            } else if ("2".equals(quarter)) {
                startTime = year + "0601000000";
                endTime = year + "0831235959";
            } else if ("3".equals(quarter)) {
                startTime = year + "0901000000";
                endTime = year + "1131235959";
            } else if ("4".equals(quarter)) {
                startTime = year + "12000000";
                int i = Integer.parseInt(year);
                int nextYear = i + 1;
                endTime = nextYear + "0231235959";
            }
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    s[1], s[2], s[8], s[0], "SHP");
            HtScanningMetadataDTO maxAreaDTO = metadataDTOList.stream()
                    .max(Comparator.comparing(HtScanningMetadataDTO::getArea))
                    .orElse(null);
            if (null == maxAreaDTO) {
                algorithmParams.put("parameter01", null);
            } else {
                String primaryResult = scanningPath + File.separator + maxAreaDTO.getFilePath();
                String maxAreaFilePath = filePath.substring(maxAreaDTO.getFilePath().lastIndexOf("/") + 1);
                String[] maxAreaSplit = maxAreaFilePath.split("_");
                params.put("primaryFile", "");
                params.put("auxPath", taskPathParam.getParameter02());
                params.put("primaryResult", primaryResult);
                params.put("clipRegion", taskPathParam.getParameter03());
                params.put("regionId", maxAreaSplit[2]);
                params.put("regionName", taskPathParam.getParameter05());
                params.put("aloType", Integer.parseInt(taskPathParam.getParameter06()));
                algorithmParams.put("parameter00", adsType);
                algorithmParams.put("parameter01", params);
            }
        }
        return algorithmParams;
    }
}
